package com.ruoyi.lxy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.lxy.domain.*;
import com.ruoyi.lxy.domain.bo.TDependantBo;
import com.ruoyi.lxy.mapper.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.lxy.domain.bo.TSelectDependantBo;
import com.ruoyi.lxy.domain.vo.TSelectDependantVo;
import com.ruoyi.lxy.service.ITSelectDependantService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 家属路线选择Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-24
 */
@RequiredArgsConstructor
@Service
public class TSelectDependantServiceImpl implements ITSelectDependantService {

    private final TSelectDependantMapper baseMapper;
    private final TTimeMapper timeMapper;
    private final TProjectMapper projectMapper  ;
    private final TRouteMapper routeMapper;

    /**
     * 查询家属路线选择
     */
    @Override
    public TSelectDependantVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询家属路线选择列表
     */
    @Override
    public TableDataInfo<TSelectDependantVo> queryPageList(TSelectDependantBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TSelectDependant> lqw = buildQueryWrapper(bo);
        Page<TSelectDependantVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询家属路线选择列表
     */
    @Override
    public List<TSelectDependantVo> queryList(TSelectDependantBo bo) {
        LambdaQueryWrapper<TSelectDependant> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TSelectDependant> buildQueryWrapper(TSelectDependantBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TSelectDependant> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDependantId() != null, TSelectDependant::getDependantId, bo.getDependantId());
        lqw.eq(bo.getTimeId() != null, TSelectDependant::getTimeId, bo.getTimeId());
        return lqw;
    }

    /**
     * 新增家属路线选择
     */
    @Override
    public Boolean insertByBo(TSelectDependantBo bo) {
        TTime tTime = timeMapper.selectById(bo.getTimeId());
        TRoute tRoute = routeMapper.selectById(tTime.getRouteId());
        TSelectDependant add = BeanUtil.toBean(bo, TSelectDependant.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
            tTime.setCount(tTime.getCount()+1);
            timeMapper.updateById(tTime);
        }
        return flag;
    }

    /**
     * 修改家属路线选择
     */
    @Override
    public Boolean updateByBo(TSelectDependantBo bo) {
        TSelectDependant update = BeanUtil.toBean(bo, TSelectDependant.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TSelectDependant entity){
        //TODO 做一些数据校验,如唯一约束
        /**
         * 判断家属是否可以选报
         */


    }

    /**
     * 批量删除家属路线选择
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
    @Override
    public Boolean deleteWithValidById(Long dependantId, long timeId,Boolean isValid){





        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        LambdaQueryWrapper<TSelectDependant> lqw= Wrappers.lambdaQuery();
        lqw.eq(TSelectDependant::getDependantId, dependantId);
        lqw.eq(TSelectDependant::getTimeId, timeId);

        boolean flag =   baseMapper.delete(lqw) > 0;
        if (flag){
            TTime tTime = timeMapper.selectById(timeId);
            tTime.setCount(tTime.getCount()-1);
            timeMapper.updateById(tTime);
        }
        return flag;
    }
}
